import store from '../store'
import { pubRequire } from '@utils/public'
import { timeToTime } from '@utils/filters'
import { Message } from 'element-ui'

const lookList = store.getters.lookList
/* 过滤返回评论内容 */
export function filterCon(item) {
  let value = item.content
  if (
    item.type === 'MARK' &&
    (value.indexOf('https') === 0 || value.indexOf('http') === 0) &&
    value.endsWith('.mp3')
  ) {
    return (
      '<audio style="width:100%;height:30px" src="' +
      value +
      '" controls="controls"></audio>'
    )
  } else {
    return contentToStr(value)
  }
}
/* 评论内容返回字符串 */
export function contentToStr(value) {
  let smileAry = getBracketStr(value)
  if (smileAry) {
    for (let item of smileAry) {
      if (lookList[item]) {
        let img =
          '<img src="' +
          lookList[item].url +
          '" alt="' +
          item +
          '"width="23px" height="23px"/>'
        let emoji = '[' + item + ']'
        value = value.replace(emoji, img)
      }
    }
  }
  return value
}
/* 获取中括号中的内容 */
export function getBracketStr(text) {
  let result = ''
  if (!text) {
    return result
  }
  let reg = /[^\[\]]+(?=\])/g
  result = text.match(reg)
  return result
}
/* 获取系统配置-保利威视 */
export function getVideoPara() {
  return new Promise((resolve, reject) => {
    let url = '/platforms/polyv/key'
    pubRequire(
      'get',
      url,
      '',
      (response) => {
        resolve(response)
      },
      (response) => {
        reject(response.message)
        Message.error(response.message)
      }
    )
  })
}
/*获取几天前的日期*/
export function getBeforDate(num) {
  let time = new Date()
  let beforeTime = time - 24 * 60 * 60 * 1000 * num
  let beforeTimeDate = new Date(beforeTime)
  let y = beforeTimeDate.getFullYear()
  let m = beforeTimeDate.getMonth() + 1
  m = m > 9 ? m : '0' + m
  let d = beforeTimeDate.getDate()
  d = d > 9 ? d : '0' + d
  return { y, m, d }
}
// 返回当前时间和七天前的23:59:59时间
export function getCreateTime(num = 7) {
  let time = new Date()
  let nowDate = timeToTime(time) // 当前时间
  let { y, m, d } = getBeforDate(num)
  let beforeDate = y + '-' + m + '-' + d + ' ' + '23:59:59' // 七天前的23:59:59
  return [beforeDate, nowDate]
}
// 返回当前时间和几天前的时分秒
export function getBeforeCurrentTime(num = 7) {
  let time = new Date()
  let timeStamp = new Date().getTime()
  let beforeStamp = timeStamp - 1000 * 60 * 60 * 24 * num
  let nowDate = timeToTime(time) // 当前时间
  let beforeDate = timeToTime(beforeStamp) //几天前的时间
  return [beforeDate, nowDate]
}
// 返回当前日期和七天（默认七天）前的日期
export function getCreateDate(num = 7) {
  let time = new Date()
  let nowDate = timeToTime(time, 'day') // 当前日期
  let { y, m, d } = getBeforDate(num)
  let beforeDate = y + '-' + m + '-' + d // 七天前的日期
  return [beforeDate, nowDate]
}
// 校验选择的时间是否在一个月内
export function validateOneMonth(startDate, endDate) {
  if (startDate && endDate) {
    let startTime = new Date(startDate)
    let endTime = new Date(endDate)
    let days = parseInt(
      (endTime.getTime() - startTime.getTime()) / (1000 * 60 * 60 * 24)
    )
    if (days > 30) {
      return false
    }
    return true
  }
}
/**
 * 获取url指定query参数
 * @param {*} url
 * @param {*} symbol // '?'、'#'符号
 * @param {*} paramName // 参数名称
 */
export function getUrlParam(url, symbol, paramName) {
  // var url = document.location.toString()
  var arrObj = url.split(symbol)

  if (arrObj.length > 1) {
    var arrPara = arrObj[1].split('&')
    var arr

    for (var i = 0; i < arrPara.length; i++) {
      arr = arrPara[i].split('=')

      if (arr != null && arr[0] === paramName) {
        return arr[1]
      }
    }
    return ''
  } else {
    return ''
  }
}
/**
 *数据统计-图表结构，x轴，根据最多显示值，返回间隔数，
 * @param {*} totalNum //所有数据总数量
 * @param {*} maxNum //x轴要求最多显示刻度个数
 * */
export function getInterval(totalNum, maxNum) {
  return Math.ceil(totalNum / maxNum - 1)
}
